#Copyright Jon Berg , turtlemeat.com

import string,cgi,time
from os import curdir, sep, popen, getcwd
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
#import pri

class MyHandler(BaseHTTPRequestHandler):

def check_installed_apps():
	

def do_GET(self):
	try:
			if self.path.endswith("/"):
				self.send_response(200)
				self.send_header('Content-type', 'text/html')
				self.end_headers()
				fileObj = open(os.getcwd()+"data/head.html","rb")
				self.wfile.write(fileObj.read()) #read the static html head and send it to the client
				fileObj.close
				if self.entry:
					self.wfile.write("<img id='cover' src='cover.jpg' width='250' height='250'/><br/>\n")
					self.wfile.write("<div id='currentSong'>"+self.artist.encode('utf-8') + " - " + self.title + "</div><br/>")					
				else:
					self.wfile.write("<div id='currentSong'> Nothing playing ...</div><br/>")			
				self.wfile.write("<input type='button' onclick=\"sendCommand('prev');\" value='<<'/>\n")
				if not globalshell.props.shell_player.get_playing():
					self.wfile.write("<input type='button' id='playButton' onclick=\"sendCommand('play');\" value='>'/>\n")
				else:
					self.wfile.write("<input type='button' id='playButton' onclick=\"sendCommand('pause');\" value='II'/>\n")
				self.wfile.write("<input type='button' onclick=\"sendCommand('next');\" value='>>'/>\n")
				self.wfile.write("</div>\n</body>\n</html>")
				return
			if self.path.endswith("/play") or self.path.endswith("/pause"):   #play
				globalshell.props.shell_player.playpause()
				self.send_response(200)
				self.send_header('Content-type', 'text/html')
				self.end_headers()
				return
			if self.path.endswith("/prev"):   #previous
				globalshell.props.shell_player.do_previous()
				self.send_response(200)
				self.send_header('Content-type', 'text/html')
				self.end_headers()
				return
			if self.path.endswith("/next"):   #next
				globalshell.props.shell_player.do_next()
				self.send_response(200)
				self.send_header('Content-type', 'text/html')
				self.end_headers()
				return
			if self.path.endswith("/artist"):
				self.send_response(200)
				self.send_header('Content-type', 'text/xml')
				self.end_headers()
				self.wfile.write(self.artist)
				return
			if self.path.endswith("/title"):
				self.send_response(200)
				self.send_header('Content-type', 'text/xml')
				self.end_headers()
				self.wfile.write(self.title)
				return
			if self.path.endswith("/isPlaying"):
				self.send_response(200)
				self.send_header('Content-type', 'text/xml')
				self.end_headers()
				if not globalshell.props.shell_player.get_playing():
					self.wfile.write("false")
				else:
					self.wfile.write("true")
				return
			if self.path.endswith("/currentSong"):
				self.send_response(200)
				self.send_header('Content-type', 'text/xml')
				self.end_headers()
				if self.entry:
					self.wfile.write(self.artist + " - " + self.title)
				else:
					self.wfile.write("Nothing playing..")
				return
			if self.path.startswith("/cover.jpg"):
				if not self.coverpath:
					self.send_error(404,'Nothing playing..')
				else:
					self.send_response(200)
					if self.coverpath.endswith(".jpg") or self.coverpath.endswith(".jpeg"):
						self.send_header('Content-type', 'image/jpeg')
					elif self.coverpath.endswith(".png"):
						self.send_header('Content-type', 'image/png')
					elif self.coverpath.endswith(".gif"):
						self.send_header('Content-type', 'image/gif')
					elif self.coverpath.endswith(".svg"):
						self.send_header('Content-type', 'image/svg+xml')
					self.end_headers()
					self.wfile.write(self.cover)
				return
			else:
				self.send_error(404,'This function has not been implemented yet: %s.' % self.path)
			return
		except Exception as e:
			self.send_error(404,'An unknown exception occured: %s' % e)

def main():
    try:
        server = HTTPServer(('', 8080), MyHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

